package com.neulogistics.contrib.procuredcenter.infrastructure.persistence.repository;

import com.neulogistics.contrib.procuredcenter.domain.model.aggregate.Vendor;
import com.neulogistics.contrib.procuredcenter.domain.repository.IVendorRepository;
import com.neulogistics.contrib.procuredcenter.infrastructure.persistence.converter.VendorEntityPOConverter;
import com.neulogistics.contrib.procuredcenter.infrastructure.persistence.orm.po.VendorPO;
import com.neulogistics.contrib.procuredcenter.infrastructure.persistence.orm.service.IVendorPOService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class VendorRepository implements IVendorRepository {
    @Resource
    IVendorPOService vendorPOService;

    @Override
    public void save(Vendor vendor) {
        VendorPO vendorPO = VendorEntityPOConverter.entityToPO(vendor);
        vendorPOService.saveOrUpdate(vendorPO);
        vendor.setVendorId(vendorPO.getVendorId());
    }

    @Override
    public Vendor findById(Integer id) {
        VendorPO vendorPO = vendorPOService.getById(id);
        Vendor vendor = VendorEntityPOConverter.POToEntity(vendorPO);
        return vendor;
    }

    @Override
    public void deleteById(Integer id) {
        vendorPOService.removeById(id);
    }
}
